/* eslint-disable */
window.fileImg =  function(e,cb) {
  let url = window.URL || window.webkitURL || window.mozURL,
    files = e.target.files;

  let orientation = 0
  let file = files[0];
  EXIF.getData(file, function () {
    orientation = EXIF.getTag(this, 'Orientation')
  })
  // 压缩图片
  let reader = new FileReader();
  reader.onload = function (e) {
    let imageEle = new Image();
    imageEle.src = e.target.result;
    imageEle.onload = function () {
      // 获取未压缩文件后缀
      let _type = file.type.split('/')[1]
      // 压缩图片宽高
      var MAX_HEIGHT = 1400;
      if (imageEle.height > MAX_HEIGHT && imageEle.height >= imageEle.width) {
        // 宽度等比例缩放 *=
        imageEle.width *= MAX_HEIGHT / imageEle.height;
        imageEle.height = MAX_HEIGHT;
      }
      //考录到用户上传的有可能是横屏图片同样过滤下宽度的图片。
      if (imageEle.width > MAX_HEIGHT && imageEle.width > imageEle.height) {
        // 宽度等比例缩放 *=
        imageEle.height *= MAX_HEIGHT / imageEle.width;
        imageEle.width = MAX_HEIGHT;
      }
      // 获取图片属性
      let
        width = imageEle.width,
        height = imageEle.height,
        canvas = document.createElement('canvas'),
        ctx = canvas.getContext("2d");
      if (orientation === 6) {
        canvas.width = height;
        canvas.height = width;
        ctx.rotate(Math.PI / 2);
        ctx.drawImage(this, 0, -height, width, height);
      } else {
        canvas.width = width;
        canvas.height = height;
        ctx.drawImage(this, 0, 0, width, height);
      }
      // 导出压缩图片
      // url = _type === 'png' ? canvas.toDataURL('image/png') : canvas.toDataURL('image/jpeg', 0.7)
         url = canvas.toDataURL('image/jpeg', 0.8)
      // return url;
      cb && cb(url)
    };
  };
  reader.readAsDataURL(file);
}